﻿using UnityEngine;
using System.Collections.Generic;

namespace Room
{

    public enum GateSubState
    {
        Closed,
        Opened,
    }


    public class RCAIGateNormal : RCAIGateBase
    {
        public int camp = 2;
        public Vector2 pos;
        public List<TweenRotation> openAnims =new List<TweenRotation>();
        public List<TweenRotation> closeAnims = new List<TweenRotation>();

        float distance = 3f;
        float curTime = 0;
        float aniTime = 0;

        bool fixedGate = true;

        bool isOpenning = false;
        bool isClosing = false;

        public GateSubState subState =  GateSubState.Closed;

        protected override void OnEnter()
        {
            camp = controller.Camp;
            distance = (float)Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionFightOpenGateDis;

            if (controller.sceneObj != null)
            {
                var aims = controller.sceneObj.GetComponentsInChildren<TweenRotation>();
                if (aims.Length > 0)
                    fixedGate = false;
                else
                    fixedGate = true;

                int i = 0;
                foreach (var aim in aims)
                {
                    if (i++ % 2 == 0)
                        openAnims.Add(aim);
                    else
                        closeAnims.Add(aim);
                }
                subState = GateSubState.Closed;
            }
        }

        protected override void OnTick()
        {
            base.OnTick();
            if (fixedGate)
            {
                return;
            }

            CheckNeedOpenGate();
        }


        void CheckNeedOpenGate()
        {
            var net = controller.GetRoomNet();
            if (net != null)
            {
                var all = net.netChariot.chariots;
                foreach (var kvp in all)
                {
                    var chariot = kvp.Value;
                    var camp = chariot.camp;
                    if (this.camp == camp)
                    {
                        var charactor = net.netChariot.GetCharactor(chariot.guid);
                        if (charactor != null)
                        {
                            
                            var pos = new Vector2(controller.owner.transform.position.x, controller.owner.transform.position.z);
                            var pos0 = new Vector2(charactor.transform.position.x, charactor.transform.position.z);
                            
                            var dis = Vector2.Distance(pos0, pos);
                            if (dis <= distance)
                            {
                                if (subState == GateSubState.Closed)
                                {
                                    subState = GateSubState.Opened;
                                    PlayOpenGateAnim();
                                    isOpenning = true;
                                    isClosing = false;
                                }
                            }
                            else
                            {
                                if (subState == GateSubState.Opened)
                                {
                                    subState = GateSubState.Closed;
                                    PlayCloseGateAnim();
                                    isClosing = true;
                                    isOpenning = false;
                                }
                            }
                        }
                    }
                }
            }
        }

    
     
        

        void PlayOpenGateAnim()
        {
            if (isOpenning)
                return;
            foreach (var aim in closeAnims)
                aim.enabled = false;
            foreach (var aim in openAnims)
            {
                aim.enabled = true;
                aim.ResetToBeginning();
                aim.PlayForward();
            }
        }

        void PlayCloseGateAnim()
        {
            if (isClosing)
                return;
            foreach (var aim in openAnims)
                aim.enabled = false;
            foreach (var aim in closeAnims)
            {
                aim.enabled = true;
                aim.ResetToBeginning();
                aim.PlayForward();
            }
        }
        
    }
}